package com.masterclient.app.modules.chezhubang;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;

import com.eoner.baselibrary.utils.CookieNewUtil;
import com.eoner.common.config.Config;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.modules.bridge.JsProductsBean;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselibrary.utils.Utils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * @auther hjian
 * @date 2019/12/4.
 */
public class CzbJsApi {

    private CzbWebActivity activity;
    public CzbJsApi(CzbWebActivity activity) {
        this.activity = activity;
    }
    @JavascriptInterface
    public void openGoodsDetail(Object productid) {
        Intent intent = new Intent(activity, NewCommodityDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", (String) productid);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @JavascriptInterface
    public void openProductDetail(Object product) {
        JsProductsBean productBean = new Gson().fromJson(product.toString(), JsProductsBean.class);
        Intent intent = new Intent(activity, NewCommodityDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", productBean.getProduct_id());
        bundle.putString("source_page",productBean.getS_product_sourcepage());
        bundle.putString("source_page_id",productBean.getS_product_sourcepage_id());
        bundle.putString("source_module",productBean.getS_product_sourcemodule());
        bundle.putString("source_position",productBean.getS_position());
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @JavascriptInterface
    public void openNativePage(Object msg) {
        try {
            JSONObject object = new JSONObject(msg.toString());
            String target = object.optString("target");
            String targetID = object.optString("targetID");
            CommonUtil.goAnyWhere(activity,  target, targetID, "", "", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public Object getUserInfo(Object msg) {
        JSONObject user = new JSONObject();
        try {
            user.put("token",SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));
            user.put("version", CookieNewUtil.getVersionName(Utils.getContext()));
            if(UserDataManager.getInstance().getUserCacheData() != null){
                user.put("mobile",UserDataManager.getInstance().getMobile());
                user.put("userId",UserDataManager.getInstance().getCustomerId());
                user.put("level",UserDataManager.getInstance().getRoleLevel());
                user.put("nickName",UserDataManager.getInstance().getNickName());
                user.put("invite",UserDataManager.getInstance().getInviteCode());
                user.put("headUrl",UserDataManager.getInstance().getHeadImgUrl());
                user.put("realName",UserDataManager.getInstance().getIdCardRealName());
                user.put("idNumber",UserDataManager.getInstance().getIdCardNo());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

    @JavascriptInterface
    public void goLogin(Object msg){
        Intent intent = new Intent(activity, QuickLoginActivity.class);
        intent.putExtra("intentSource",activity.title);
        activity.startActivityForResult(intent,0);
    }
    //同步API
    @JavascriptInterface
    public String getToken(Object msg) {
        String token = SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN);
        return token;
    }

    @JavascriptInterface
    public void mapNavigate(Object obj){
        Map<String,String> map = new Gson().fromJson(obj.toString(),Map.class);
       if ( activity.dialog != null){
           activity.setDialog(map);
       }
    }

}
